<template>
	<view>
		<!-- 扫码页面 -->
		<!-- #ifndef APP-PLUS -->
		<view class="wrap">
			<view class="u-tips-color">
				请在app中打开
			</view>
		</view>
		<!-- #endif -->
		
	</view>
</template>
<script>
	import SM4Util from '@/utils/sm4/index.js'
	import request from "@/utils/request.js";
	import {
		getCache,
		setCache,
		apiUrl,
		yfAPIS
	} from "@/utils/cache.js";
	export default {
		data() {
			return {
				barcode: null,
				flash: false,
				tip: '将二维码放入框中，即可自动扫描',
				isShow: true,

				faceUrl: "",
				secretKey: 'trE4n9TqGZyiC5aP',
				aId:'',
				entroce:'',
			}
		},
		onShow() {
			// 页面展示时，重新启动扫描检测
			if (this.barcode) {
				this.barcode.start()
			}
		},
		onLoad(params) {
			const {
				tip,
				aId
			} = params
			if (tip) {
				this.tip = tip
			}
			if (aId && aId.length > 0) {
				this.aId = aId;
				const infoData = getCache(yfAPIS);
				this.entroce = infoData.entroce;
				// #ifdef APP-PLUS
				plus.navigator.setFullscreen(true); //全屏
				let currentWebview = this.$scope.$getAppWebview();
				this.createBarcode(currentWebview)
				this.createTipInfoView(currentWebview)
				this.createFlashBarView(currentWebview)
				// #endif
			} else {
				this.showMessage('活动id没有值!');
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}

		},
		mounted() {},
		methods: {
			//普通纸质票
			async regular( qrCodeData) {
				const obj = {
					aid: this.aId,
					code: '20000',
					verificationData: qrCodeData
				}
				const data = await request.post('/app-verification/v1/device/modes/allInOneMachine', obj);
				if (data.status === 200) {
					if (data.data.code === '1') {
						const objqr = {
							aid: this.aId,
							code: '20001',
							verificationData: qrCodeData,
							facePic: this.faceUrl,
							entrance: this.entroce,
							deviceSn: 'fe80::da04:fda5:cb61:'
						}
						const dataQr = await request.post('/app-verification/v1/device/modes/allInOneMachine', objqr);
						if (dataQr.status === 200) {
							if(dataQr.data.code === '1'){
								this.showMessage(1,dataQr.message)
							}else{
								this.showMessage(0,dataQr.message)
							}
							
						}
					} else {
						this.showMessage(0,data.message)
					}
				}
			},
			//登记票
			async register(jieStr, qrCodeData) {
				const dataInfo = jieStr.split('|');
				if (dataInfo.length === 3 && dataInfo[2].length > 0) {
					const cardNum = SM4Util.sm4ECBEncrypt(dataInfo[2], this.secretKey);
					const obj = {
						aid: this.aId,
						code: '10001',
						deviceSn: 'fe80::da04:fda5:cb61:',
						entrance: this.entroce,
						facePic: this.faceUrl,
						allOneUserIdCard: {
							cardNum: cardNum,
							name: dataInfo[1],
							cardPic: ''
						},
					}
					const data = await request.post('/app-verification/v1/device/modes/allInOneMachine', obj)
					if (data.status === 200) {
						if(data.data.code === '1'){
							console.log('asdfasd',data);
							this.showMessage(1,data.message)
						}else{
							this.showMessage(0,data.message)
						}
						
					}

				}
			},

			qrCodeApi(qrCodeData) {
				let miStr = qrCodeData;
				const typeData = miStr.substr(0, 5);
				if (typeData === 'YfLeZ') {
					//登记模式
					const codeInfo = miStr.slice(5);
					const jieStr = SM4Util.sm4ECBDecrypt(codeInfo, this
					.secretKey); // 解密 SM4Util.sm4ECBDecrypt(加密后的字符串, 密钥)
					this.register(jieStr, qrCodeData);
				} else {
					//普通纸质票
					this.regular(qrCodeData)
				}
			},


		
			/** * 创建二维码 * @param {Object} currentWebview */
			createBarcode(currentWebview) {
				if (!this.barcode) {
					this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
						top: `0px`,
						left: '0px',
						height: `100%`,
						width: '100%',
						position: 'absolute',
						background: '#FFCC00',
						frameColor: '#FFCC33',
						scanbarColor: '#FFCC33',
					});
					this.barcode.onmarked = this.onmarked;
					this.barcode.setFlash(this.flash);
					//此处未演示扫码成功回调的地址设置，实际请参考HTML5Plus API自行处理 
					//注意扫码区域需为正方形，否则影响扫码识别率 
					currentWebview.append(this.barcode);
				}
				this.barcode.start()
			},
			/** * 创建提示信息 * @param {Object} currentWebview */
			createTipInfoView(currentWebview) {
				const content = new plus.nativeObj.View('content', {
						top: '0px',
						left: '0px',
						height: '100%',
						width: '100%'
					},
					[{
						tag: 'font',
						id: 'scanTips',
						text: this.tip,
						textStyles: {
							size: '14px',
							color: '#ffffff',
							whiteSpace: 'normal'
						},
						position: {
							top: '90px',
							left: '10%',
							width: '80%',
							height: 'wrap_content'
						}
					}]);
				currentWebview.append(content);
			},
			// 创建 开关灯按钮
			createFlashBarView(currentWebview) {
				const openImg = this.crtFlashImg('static/img/open.png', '轻触关闭')
				const closeImg = this.crtFlashImg('static/img/close.png', '轻触照亮')
				const scanBarVew = new plus.nativeObj.View('scanBarVew', {
						bottom: '15%',
						left: '40%',
						height: '10%',
						width: '20%',
					},
					closeImg);
				scanBarVew.interceptTouchEvent(true);
				currentWebview.append(scanBarVew);
				scanBarVew.addEventListener("click", (e) => { //点亮手电筒
					this.flash = !this.flash;
					if (this.flash) {
						scanBarVew.draw(openImg);
					} else {
						scanBarVew.draw(closeImg)
					}
					if (this.barcode) {
						this.barcode.setFlash(this.flash);
					}
				}, false)
			},
			crtFlashImg(imgsrc, val) {
				return [{
					tag: 'img',
					id: 'scanBar',
					src: imgsrc,
					position: {
						width: '28%',
						left: '36%',
						height: '30%'
					}
				}, {
					tag: 'font',
					id: 'font',
					text: val,
					textStyles: {
						size: '10px',
						color: '#ffffff'
					},
					position: {
						width: '80%',
						left: '10%'
					}
				}]
			},
			// 扫码成功回调
			onmarked(type, result) {
				let that = this;
				console.log('条码类型：' + type);
				console.log('条码内容 ：' + result);
				this.qrCodeApi(result);
				// uni.showToast({
				// 	title:'通过',
				// 	image:'/static/img/success.png',
				// 	icon:'success',
				// 	duration:1500,
				// })	
			},
			showMessage(type,message) {
				let that = this;
				let img = 'warn'
				if(type === 1){
					message = '通过!'
					img = 'success'
				}
				uni.showToast({
					title: message,
					duration: 1500,
					icon: 'none',
					image:'/static/img/'+img+'.png',
				})
				setTimeout(() => {
					if (that.barcode) {
						that.barcode.start()
					}
				},1500)
			},
		}
	}
</script>
<style scoped>
	.wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
</style>